<?php

class Helper {
	private static $countries = array("Afghanistan" => "Afghanistan", "Aland Islands" => "Aland Islands", "Albania" => "Albania", "Algeria" => "Algeria", "American Samoa" => "American Samoa", "Andorra" => "Andorra", "Angola" => "Angola",
 	          "Anguilla" => "Anguilla", "Antarctica" => "Antarctica", "Antigua And Barbuda" => "Antigua And Barbuda", "Argentina" => "Argentina", "Armenia" => "Armenia", "Aruba" => "Aruba", "Australia" => "Australia", "Austria" => "Austria",
 	          "Azerbaijan" => "Azerbaijan", "Bahamas" => "Bahamas", "Bahrain" => "Bahrain", "Bangladesh" => "Bangladesh", "Barbados" => "Barbados", "Belarus" => "Belarus", "Belgium" => "Belgium", "Belize" => "Belize", "Benin" => "Benin",
 	          "Bermuda" => "Bermuda", "Bhutan" => "Bhutan", "Bolivia" => "Bolivia", "Bosnia and Herzegowina" => "Bosnia and Herzegowina", "Botswana" => "Botswana", "Bouvet Island" => "Bouvet Island", "Brazil" => "Brazil",
 	          "British Indian Ocean Territory" => "British Indian Ocean Territory", "Brunei Darussalam" => "Brunei Darussalam", "Bulgaria" => "Bulgaria", "Burkina Faso" => "Burkina Faso", "Burundi" => "Burundi", "Cambodia" => "Cambodia",
 	          "Cameroon" => "Cameroon", "Canada" => "Canada", "Cape Verde" => "Cape Verde", "Cayman Islands" => "Cayman Islands", "Central African Republic" => "Central African Republic", "Chad" => "Chad", "Chile" => "Chile", "China" => "China",
 	          "Christmas Island" => "Christmas Island", "Cocos (Keeling) Islands", "Colombia" => "Colombia", "Comoros" => "Comoros", "Congo" => "Congo",
 	          "Congo, the Democratic Republic of the", "Cook Islands" => "Cook Islands", "Costa Rica" => "Costa Rica", "Cote d'Ivoire", "Croatia" => "Croatia", "Cuba" => "Cuba",
 	          "Cyprus" => "Cyprus", "Czech Republic" => "Czech Republic", "Denmark" => "Denmark", "Djibouti" => "Djibouti", "Dominica" => "Dominica", "Dominican Republic" => "Dominican Republic", "Ecuador" => "Ecuador", "Egypt" => "Egypt",
 	          "El Salvador" => "El Salvador", "Equatorial Guinea" => "Equatorial Guinea", "Eritrea" => "Eritrea", "Estonia" => "Estonia", "Ethiopia" => "Ethiopia", "Falkland Islands (Malvinas)",
 	          "Faroe Islands" => "Faroe Islands", "Fiji" => "Fiji", "Finland" => "Finland", "France" => "France", "French Guiana" => "French Guiana", "French Polynesia" => "French Polynesia",
 	          "French Southern Territories" => "French Southern Territories", "Gabon" => "Gabon", "Gambia" => "Gambia", "Georgia" => "Georgia", "Germany" => "Germany", "Ghana" => "Ghana", "Gibraltar" => "Gibraltar", "Greece" => "Greece", "Greenland" => "Greenland", "Grenada" => "Grenada", "Guadeloupe" => "Guadeloupe", "Guam" => "Guam", "Guatemala" => "Guatemala", "Guernsey" => "Guernsey", "Guinea" => "Guinea",
 	          "Guinea-Bissau", "Guyana" => "Guyana", "Haiti" => "Haiti", "Heard and McDonald Islands" => "Heard and McDonald Islands", "Holy See (Vatican City State)",
 	          "Honduras" => "Honduras", "Hong Kong" => "Hong Kong", "Hungary" => "Hungary", "Iceland" => "Iceland", "India" => "India", "Indonesia" => "Indonesia", "Iran, Islamic Republic of", "Iraq" => "Iraq",
 	          "Ireland" => "Ireland", "Isle of Man" => "Isle of Man", "Israel" => "Israel", "Italy" => "Italy", "Jamaica" => "Jamaica", "Japan" => "Japan", "Jersey" => "Jersey", "Jordan" => "Jordan", "Kazakhstan" => "Kazakhstan", "Kenya" => "Kenya",
 	          "Kiribati" => "Kiribati", "Korea, Democratic People's Republic of", "Korea, Republic of", "Kuwait" => "Kuwait", "Kyrgyzstan" => "Kyrgyzstan",
 	          "Lao People's Democratic Republic", "Latvia" => "Latvia", "Lebanon" => "Lebanon", "Lesotho" => "Lesotho", "Liberia" => "Liberia", "Libyan Arab Jamahiriya" => "Libyan Arab Jamahiriya",
 	          "Liechtenstein" => "Liechtenstein", "Lithuania" => "Lithuania", "Luxembourg" => "Luxembourg", "Macao" => "Macao", "Macedonia, The Former Yugoslav Republic Of",
 	          "Madagascar" => "Madagascar", "Malawi" => "Malawi", "Malaysia" => "Malaysia", "Maldives" => "Maldives", "Mali" => "Mali", "Malta" => "Malta", "Marshall Islands" => "Marshall Islands", "Martinique" => "Martinique",
 	          "Mauritania" => "Mauritania", "Mauritius" => "Mauritius", "Mayotte" => "Mayotte", "Mexico" => "Mexico", "Micronesia, Federated States of", "Moldova, Republic of",
 	          "Monaco" => "Monaco", "Mongolia" => "Mongolia", "Montenegro" => "Montenegro", "Montserrat" => "Montserrat", "Morocco" => "Morocco", "Mozambique" => "Mozambique", "Myanmar" => "Myanmar", "Namibia" => "Namibia", "Nauru" => "Nauru",
 	          "Nepal" => "Nepal", "Netherlands" => "Netherlands", "Netherlands Antilles" => "Netherlands Antilles", "New Caledonia" => "New Caledonia", "New Zealand" => "New Zealand", "Nicaragua" => "Nicaragua", "Niger" => "Niger",
 	          "Nigeria" => "Nigeria", "Niue" => "Niue", "Norfolk Island" => "Norfolk Island", "Northern Mariana Islands" => "Northern Mariana Islands", "Norway" => "Norway", "Oman" => "Oman", "Pakistan" => "Pakistan", "Palau" => "Palau",
 	          "Palestinian Territory, Occupied", "Panama" => "Panama", "Papua New Guinea" => "Papua New Guinea", "Paraguay" => "Paraguay", "Peru" => "Peru", "Philippines" => "Philippines",
 	          "Pitcairn" => "Pitcairn", "Poland" => "Poland", "Portugal" => "Portugal", "Puerto Rico" => "Puerto Rico", "Qatar" => "Qatar", "Reunion" => "Reunion", "Romania" => "Romania", "Russian Federation" => "Russian Federation",
 	          "Rwanda" => "Rwanda", "Saint Barthelemy" => "Saint Barthelemy", "Saint Helena" => "Saint Helena", "Saint Kitts and Nevis" => "Saint Kitts and Nevis", "Saint Lucia" => "Saint Lucia",
 	          "Saint Pierre and Miquelon" => "Saint Pierre and Miquelon", "Saint Vincent and the Grenadines" => "Saint Vincent and the Grenadines", "Samoa" => "Samoa", "San Marino" => "San Marino",
 	          "Sao Tome and Principe" => "Sao Tome and Principe", "Saudi Arabia" => "Saudi Arabia", "Senegal" => "Senegal", "Serbia" => "Serbia", "Seychelles" => "Seychelles", "Sierra Leone" => "Sierra Leone", "Singapore" => "Singapore",
 	          "Slovakia" => "Slovakia", "Slovenia" => "Slovenia", "Solomon Islands" => "Solomon Islands", "Somalia" => "Somalia", "South Africa" => "South Africa",
 	          "South Georgia and the South Sandwich Islands" => "South Georgia and the South Sandwich Islands", "Spain" => "Spain", "Sri Lanka" => "Sri Lanka", "Sudan" => "Sudan", "Suriname" => "Suriname",
 	          "Svalbard and Jan Mayen" => "Svalbard and Jan Mayen", "Swaziland" => "Swaziland", "Sweden" => "Sweden", "Switzerland" => "Switzerland", "Syrian Arab Republic" => "Syrian Arab Republic",
 	          "Taiwan, Province of China", "Tajikistan" => "Tajikistan", "Tanzania, United Republic of", "Thailand" => "Thailand", "Timor-Leste",
 	          "Togo" => "Togo", "Tokelau" => "Tokelau", "Tonga" => "Tonga", "Trinidad and Tobago" => "Trinidad and Tobago", "Tunisia" => "Tunisia", "Turkey" => "Turkey", "Turkmenistan" => "Turkmenistan",
 	          "Turks and Caicos Islands" => "Turks and Caicos Islands", "Tuvalu" => "Tuvalu", "Uganda" => "Uganda", "Ukraine" => "Ukraine", "United Arab Emirates" => "United Arab Emirates", "United Kingdom" => "United Kingdom",
 	          "United States" => "United States", "United States Minor Outlying Islands" => "United States Minor Outlying Islands", "Uruguay" => "Uruguay", "Uzbekistan" => "Uzbekistan", "Vanuatu" => "Vanuatu", "Venezuela" => "Venezuela",
 	          "Viet Nam" => "Viet Nam", "Virgin Islands, British", "Virgin Islands, U.S.", "Wallis and Futuna" => "Wallis and Futuna", "Western Sahara" => "Western Sahara",
 	          "Yemen" => "Yemen", "Zambia" => "Zambia", "Zimbabwe" => "Zimbabwe");
	private static $states = array(
		'United States' => array("Alabama" => "Alabama","Alaska" => "Alaska","American Samoa" => "American Samoa","Arizona" => "Arizona","Arkansas" => "Arkansas","California" => "California","Colorado" => "Colorado",
			"Connecticut" => "Connecticut","Delaware" => "Delaware","District of Columbia" => "District of Columbia","Florida" => "Florida","Georgia" => "Georgia","Guam" => "Guam","Hawaii" => "Hawaii","Idaho" => "Idaho","Illinois" => "Illinois",
			"Indiana" => "Indiana","Iowa" => "Iowa","Kansas" => "Kansas","Kentucky" => "Kentucky","Louisiana" => "Louisiana","Maine" => "Maine","Maryland" => "Maryland","Massachusetts" => "Massachusetts","Michigan" => "Michigan","Minnesota" => "Minnesota",
			"Mississippi" => "Mississippi","Missouri" => "Missouri","Montana" => "Montana","Nebraska" => "Nebraska","Nevada" => "Nevada","New Hampshire" => "New Hampshire","New Jersey" => "New Jersey","New Mexico" => "New Mexico","New York" => "New York",
			"North Carolina" => "North Carolina","North Dakota" => "North Dakota","Northern Marianas Islands" => "Northern Marianas Islands","Ohio" => "Ohio","Oklahoma" => "Oklahoma","Oregon" => "Oregon","Pennsylvania" => "Pennsylvania",
			"Puerto Rico" => "Puerto Rico","Rhode Island" => "Rhode Island","South Carolina" => "South Carolina","South Dakota" => "South Dakota","Tennessee" => "Tennessee","Texas" => "Texas","Utah" => "Utah","Vermont" => "Vermont","Virginia" => "Virginia",
			"Virgin Islands" => "Virgin Islands","Washington" => "Washington","West Virginia" => "West Virginia","Wisconsin" => "Wisconsin","Wyoming" => "Wyoming"),
		'Canada' => array("Alberta" => "Alberta","British Columbia" => "British Columbia","Manitoba" => "Manitoba","New Brunswick" => "New Brunswick","Newfoundland" => "Newfoundland","Northwest Territories" => "Northwest Territories",
			"Nunavut" => "Nunavut","Nova Scotia" => "Nova Scotia","Ontario" => "Ontario","Prince Edward Island" => "Prince Edward Island","Quebec" => "Quebec","Saskatchewan" => "Saskatchewan","Yukon" => "Yukon"),
		'Italy' => array("Agrigento" => "Agrigento","Alessandria" => "Alessandria","Ancona" => "Ancona","Aosta" => "Aosta","Arezzo" => "Arezzo","Ascoli Piceno" => "Ascoli Piceno","Asti" => "Asti","Avellino" => "Avellino","Bari" => "Bari","Barletta-Andria-Trani",
			"Belluno" => "Belluno","Benevento" => "Benevento","Bergamo" => "Bergamo","Biella" => "Biella","Bologna" => "Bologna","Bolzano" => "Bolzano","Brescia" => "Brescia","Brindisi" => "Brindisi","Cagliari" => "Cagliari","Caltanissetta" => "Caltanissetta",
			"Campobasso" => "Campobasso","Carbonia-Iglesias","Caserta" => "Caserta","Catania" => "Catania","Catanzaro" => "Catanzaro","Chieti" => "Chieti","Como" => "Como","Cosenza" => "Cosenza","Cremona" => "Cremona","Crotone" => "Crotone",
			"Cuneo" => "Cuneo","Enna" => "Enna","Fermo" => "Fermo","Ferrara" => "Ferrara","Firenze" => "Firenze","Foggia" => "Foggia","ForlÃ¬-Cesena","Frosinone" => "Frosinone","Genova" => "Genova",
			"Gorizia" => "Gorizia","Grosseto" => "Grosseto","Imperia" => "Imperia","Isernia" => "Isernia","L'Aquila","La Spezia" => "La Spezia","Latina" => "Latina","Lecce" => "Lecce","Lecco" => "Lecco","Livorno" => "Livorno","Lodi" => "Lodi","Lucca" => "Lucca",
			"Macerata" => "Macerata","Mantova" => "Mantova","Massa-Carrara","Matera" => "Matera","Medio Campidano" => "Medio Campidano","Messina" => "Messina","Milano" => "Milano","Modena" => "Modena","Monza e Brianza" => "Monza e Brianza",
			"Napoli" => "Napoli","Novara" => "Novara","Nuoro" => "Nuoro","Ogliastra" => "Ogliastra","Olbia-Tempio","Oristano" => "Oristano","Padova" => "Padova","Palermo" => "Palermo","Parma" => "Parma","Pavia" => "Pavia","Perugia" => "Perugia","Pesaro e Urbino" => "Pesaro e Urbino",
			"Pescara" => "Pescara","Piacenza" => "Piacenza","Pisa" => "Pisa","Pistoia" => "Pistoia","Pordenone" => "Pordenone","Potenza" => "Potenza","Prato" => "Prato","Ragusa" => "Ragusa","Ravenna" => "Ravenna","Reggio Calabria" => "Reggio Calabria","Reggio Emilia" => "Reggio Emilia","Rieti" => "Rieti",
			"Rimini" => "Rimini","Roma" => "Roma","Rovigo" => "Rovigo","Salerno" => "Salerno","Sassari" => "Sassari","Savona" => "Savona","Siena" => "Siena","Sondrio" => "Sondrio","Siracusa" => "Siracusa","Taranto" => "Taranto","Teramo" => "Teramo","Terni" => "Terni","Trapani" => "Trapani","Trento" => "Trento",
			"Treviso" => "Treviso","Trieste" => "Trieste","Torino" => "Torino","Udine" => "Udine","Varese" => "Varese","Venezia" => "Venezia","Verbano-Cusio-Ossola","Vercelli" => "Vercelli","Verona" => "Verona","Vibo Valentia" => "Vibo Valentia","Vicenza" => "Vicenza",
			"Viterbo" => "Viterbo"),
		'Spain' => array("Álava/Araba","Albacete" => "Albacete","Alicante/Alacant","Almería","Asturias" => "Asturias","Ávila","Badajoz" => "Badajoz","Barcelona" => "Barcelona",
			"Biscay" => "Biscay","Burgos" => "Burgos","Cáceres","Cádiz","Cantabria" => "Cantabria","Castellón/Castelló","Ceuta" => "Ceuta","Ciudad" => "Ciudad","Córdoba","La Cuenca" => "La Cuenca",
			"Girona" => "Girona","Granada" => "Granada","Guadalajara" => "Guadalajara","Guipúzcoa/Gipuzkoa","Huelva" => "Huelva","Huesca" => "Huesca","Islas Baleares/Illes Balears (Balearic Islands)",
			"Jaén","La Rioja" => "La Rioja","León","Lérida/Lleida","Lugo" => "Lugo","Madrid" => "Madrid","Málaga","Melilla" => "Melilla","Murcia" => "Murcia","Navarra/Nafarroa (Navarre)",
			"Orense/Ourense","Palencia" => "Palencia","Las Palmas" => "Las Palmas","Pontevedra" => "Pontevedra","Salamanca" => "Salamanca","Santa Cruz de Tenerife" => "Santa Cruz de Tenerife","Segovia" => "Segovia","Sevilla (Seville)",
			"Soria" => "Soria","Tarragona" => "Tarragona","Teruel" => "Teruel","Toledo" => "Toledo","Valencia/València","Valladolid" => "Valladolid","Zamora" => "Zamora","Zaragoza" => "Zaragoza"), 
		'Germany' => array("Baden-Württemberg","Bayern" => "Bayern","Brandenburg" => "Brandenburg","Hamburg" => "Hamburg","Hessen" => "Hessen","Mecklenburg-Vorpommern",
			"Niedersachsen" => "Niedersachsen","Nordrhein-Westfalen","Rheinland-Pfalz","Saarland" => "Saarland","Sachsen" => "Sachsen","Sachsen-Anhalt",
			"Schleswig-Holstein","Thüringen"), 
		'France' => array(),
		'United Kingdom' => array()
	);
	/**
	 * Return the path to the images folder for the current theme
	 *
	 * @return String
	 */
	function imagePath(){
		return base_path().path_to_theme()."/images/";
	}
	/**
	 * Return an image tag with options
	 * Defaults:
	 * - alt => $image
	 *
	 * @param String $image Image name
	 * @param Array $options Options
	 * @return String
	 */
	function image($image,$options=array()){
		$options = array_merge(array(
			'alt' => $image,
			'src' => self::imagePath().$image
		),$options);
		return Helper::tag('img',$options);
	}
	/**
	 * Return an HTML tag
	 *
	 * @param String $name Tag
	 * @param Array $options Attributes
	 * @return String
	 */
	function tag($name,$options=array()){
		return "<$name ".self::applyTagOptions($options)." />";
	}
	/**
	 * Return an HTML tag with content inside
	 *
	 * @param String $name Tag
	 * @param String $content HTML content
	 * @param Array $options Array of options
	 * @return String
	 */
	function contentTag($name,$content,$options=array()){
		return "<{$name} ".self::applyTagOptions($options).">{$content}</{$name}";
	}
	function addTextField($form,$model,$field,$options = array()){
		$form[$model."[".$field."]"] = array_merge(array(
			'#type' => 'textfield',
	  		'#title' => t(ucwords(Inflector::humanize($field))),
	  		'#default_value' => self::getValue($model,$field),
	  		'#size' => 60,
	  		'#maxlength' => 64,
			'#suffix' => "<br class=\"clear\" />"),
		$options);
		return $form;
	}
	function addPasswordField($form,$model,$field,$options = array()){
		$form[$model."[".$field."]"] = array_merge(array(
			'#type' => 'password',
	  		'#title' => t(ucwords(Inflector::humanize($field))),
	  		'#default_value' => '',
	  		'#size' => 60,
	  		'#maxlength' => 64,
			'#suffix' => "<br class=\"clear\" />"
		),$options);
		return $form;
	}
	function addCountrySelect($form,$model,$field,$options = array()){
		$form[$model."[".$field."]"] = array_merge(array(
			'#type' => 'select',
			'#options' => self::$countries,
	  		'#title' => t(ucwords(Inflector::humanize($field))),
	  		'#default_value' => self::getValue($model,$field),
			'#suffix' => "<br class=\"clear\" />"
		),$options);
		return $form;
	}
	function addStatesSelect($form,$model,$field,$country,$options = array()){
		$form[$model."[".$field."]"] = array_merge(array(
			'#type' => 'select',
			'#options' => self::$states[$country],
	  		'#title' => t(ucwords(Inflector::humanize($field))),
	  		'#default_value' => self::getValue($model,$field),
			'#suffix' => "<br class=\"clear\" />"
		),$options);
		return $form;
	}
	function addHiddenField($form,$model,$field,$options = array()){
		$form[$model."[".$field."]"] = array_merge(array(
			'#type' => 'hidden',
	  		'#default_value' => self::getValue($model,$field)
		),$options);
		return $form;
	}
	private function getValue($model,$field){
		eval("global \$$model;");
		eval("\$val = \$".$model."->".$field.";");
		return $val;
	}
	private function applyTagOptions($options){
		$ret = '';
		foreach($options as $key => $val){
			$ret .= "{$key}=\"{$val}\" ";
		}
		return $ret;
	}
}

?>